import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/config/sqflite/user_db_provider.dart';
import 'package:flutter_douban/main_page/home/h5_detail_page/activity_detail_page.dart';
import 'package:flutter_douban/tool_views/back_nav_btn.dart';
import 'package:flutter_douban/tool_views/ios_loading_dialog.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'code_login_page.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  FocusNode blankNode = FocusNode();
  late TextEditingController _phoneTextEditingController;
  late TextEditingController _codeTextEditingController;
  late TextEditingController _passTextEditingController;
  String _codeStr = '';
  String _phoneStr = '';
  String _passStr = '';
  bool firstShow = true;
  bool secondShow = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _phoneTextEditingController = TextEditingController();
    _codeTextEditingController = TextEditingController();
    _passTextEditingController = TextEditingController();

    _phoneTextEditingController.addListener(() {
      setState(() {
        _phoneStr = _phoneTextEditingController.text;
      });
    });
    _codeTextEditingController.addListener(() {
      setState(() {
        _codeStr = _codeTextEditingController.text;
      });
    });
    _passTextEditingController.addListener(() {
      setState(() {
        _passStr = _passTextEditingController.text;
      });
    });
  }

  /// 注册方法
  void registerMethoed() {
    if (_phoneTextEditingController.text.isEmpty) {
      Fluttertoast.showToast(
          msg: "请输入您的电话号码~",
          toastLength: Toast.LENGTH_LONG,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black87,
          textColor: Colors.white,
          fontSize: 14.0);
      return;
    }

    if (_phoneTextEditingController.text.isNotEmpty &&
        _phoneTextEditingController.text.length != 11) {
      Fluttertoast.showToast(
          msg: "请输入正确的电话号码~",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black87,
          textColor: Colors.white,
          fontSize: 14.0);
      return;
    }

    if (_codeTextEditingController.text.isEmpty ||
        _passTextEditingController.text.isEmpty) {
      Fluttertoast.showToast(
          msg: "请输入您的密码~",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black87,
          textColor: Colors.white,
          fontSize: 14.0);
      return;
    }

    if (_codeTextEditingController.text != _passTextEditingController.text) {
      Fluttertoast.showToast(
          msg: "两次输入密码不一致~",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black87,
          textColor: Colors.white,
          fontSize: 14.0);
      return;
    }

    if (_phoneTextEditingController.text.isNotEmpty &&
        _phoneTextEditingController.text.length == 11) {
      UserDbProvider provider = new UserDbProvider();
      List _list;
      provider.getUserData(_phoneStr).then((value) => {
            _list = value,
            if (_list.length > 0)
              {
                /// 用户已经注册
                Fluttertoast.showToast(
                    msg: "账户已注册,去登录~",
                    toastLength: Toast.LENGTH_LONG,
                    gravity: ToastGravity.CENTER,
                    timeInSecForIosWeb: 1,
                    backgroundColor: Colors.black87,
                    textColor: Colors.white,
                    fontSize: 14.0),
                Loading.hide(context),
                Navigator.pop(context),
              }
          });

      /// 注册账号数据插入数据库
      provider.insert(_phoneStr, _codeStr);
      Loading.show(context);
      Future.delayed(const Duration(milliseconds: 1000), () {
        Loading.hide(context);

        /// 返回登录
        Navigator.pop(context);
        Fluttertoast.showToast(
            msg: "注册成功,请登录~",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.CENTER,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.black87,
            textColor: Colors.white,
            fontSize: 14.0);
      });
      return;
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _phoneTextEditingController.dispose();
    _codeTextEditingController.dispose();
    _passTextEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leadingWidth: 45,
          leading: BackNavBtn(),
          actions: [
            IconButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                    return ActivityDetail(
                      url:
                          'https://m.douguo.com/help/vip?open=0&aggressive=QrDdqKtzWQ&clientkey=2895b5a15d4202ab&app-state=0&device=iPhone9,1&device_id=13CB3E69-C216-4B9B-A995-1946AB7B9699&imei=4926D5D3-18DB-48D7-B85A-D065350C3BA5&pset=1&sdk=14.4&timestamp=1637654506&version=7100',
                      titleStr: '账号和VIP权益',
                    );
                  }));
                },
                alignment: Alignment.centerLeft,
                icon: Image.asset(
                  'assets/images/common/login_help@2x.png',
                  width: 22,
                  height: 22,
                  color: Colors.black,
                )),
          ],
        ),
        body: Container(
          color: Colors.white,
          child: GestureDetector(
              onTap: () {
                // 点击空白页面关闭键盘
                FocusScope.of(context).requestFocus(blankNode);
              },
              child: SingleChildScrollView(
                child: Container(
                  color: Colors.white,
                  alignment: Alignment.centerLeft,
                  height: MediaQuery.of(context).size.height,
                  padding: EdgeInsets.only(left: 30, right: 30),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: 20,
                      ),
                      Text(
                        '注册',
                        style: TextStyle(
                            fontSize: 26,
                            fontWeight: FontWeight.bold,
                            color: Colors.black),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Text(
                        'Hi,欢迎来到豆果美食会做饭很酷~',
                        style: TextStyle(fontSize: 14, color: Colors.black26),
                      ),
                      SizedBox(
                        height: 60,
                      ),
                      Row(
                        children: [
                          Row(
                            children: [
                              Text(
                                '+86',
                                style: TextStyle(
                                    fontSize: 15, color: Colors.black),
                              ),
                              SizedBox(
                                width: 5,
                              ),
                              Image.asset(
                                'assets/images/common/icon_topic_down@2x.png',
                                width: 14,
                                height: 14,
                                color: Colors.black,
                              )
                            ],
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Container(
                            height: 60,
                            color: Colors.white,
                            width: MediaQuery.of(context).size.width - 160,
                            alignment: Alignment.center,
                            child: TextField(
                              autofocus: true,
                              maxLines: 1,
                              keyboardType: TextInputType.phone,
                              controller: _phoneTextEditingController,
                              decoration: InputDecoration.collapsed(
                                  hintText: '请输入手机号',
                                  hintStyle: TextStyle(
                                    fontSize: 13,
                                  )),
                            ),
                          )
                        ],
                      ),
                      Container(
                        height: 1,
                        color: AppColors.line,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            height: 60,
                            width: MediaQuery.of(context).size.width - 160,
                            alignment: Alignment.center,
                            child: TextField(
                              autofocus: true,
                              obscureText: firstShow,
                              controller: _codeTextEditingController,
                              decoration: InputDecoration.collapsed(
                                  hintText: '请输入密码',
                                  hintStyle: TextStyle(
                                    fontSize: 13,
                                  )),
                            ),
                          ),
                          IconButton(
                              onPressed: () {
                                setState(() {
                                  firstShow = !firstShow;
                                });
                              },
                              icon: Image.asset(
                                'assets/images/common/icon_home_eye@2x.png',
                                width: 18,
                                height: 18,
                                color: firstShow == true
                                    ? Colors.grey
                                    : Colors.blue,
                              ))
                        ],
                      ),
                      Container(
                        height: 1,
                        color: AppColors.line,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            height: 60,
                            width: MediaQuery.of(context).size.width - 160,
                            alignment: Alignment.center,
                            child: TextField(
                              autofocus: true,
                              controller: _passTextEditingController,
                              obscureText: secondShow,
                              decoration: InputDecoration.collapsed(
                                  hintText: '请再次输入密码',
                                  hintStyle: TextStyle(
                                    fontSize: 13,
                                  )),
                            ),
                          ),
                          IconButton(
                              onPressed: () {
                                setState(() {
                                  secondShow = !secondShow;
                                });
                              },
                              icon: Image.asset(
                                'assets/images/common/icon_home_eye@2x.png',
                                width: 18,
                                height: 18,
                                color: secondShow == true
                                    ? Colors.grey
                                    : Colors.blue,
                              ))
                        ],
                      ),
                      Container(
                        height: 1,
                        color: AppColors.line,
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      InkWell(
                        child: Container(
                          alignment: Alignment.center,
                          height: 44,
                          width: MediaQuery.of(context).size.width,
                          decoration: BoxDecoration(
                            color: (_phoneStr.length == 11 &&
                                    _codeStr.length > 0 &&
                                    _passStr.length > 0)
                                ? AppColors.yellow
                                : AppColors.line,
                            borderRadius: BorderRadius.circular(22),
                          ),
                          child: Text(
                            '注册',
                            style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.bold,
                                color: (_phoneStr.length == 11 &&
                                        _codeStr.length > 0 &&
                                        _passStr.length > 0)
                                    ? AppColors.black
                                    : AppColors.qianTextColor),
                          ),
                        ),
                        onTap: registerMethoed,
                      ),
                    ],
                  ),
                ),
              )),
        ));
  }
}
